package com.lc202406;

public class Solution682 {
    public static int calPoints(String[] operations) {
        int length = operations.length;
        int[] arr = new int[length];
        int index = 0;
        for (String operation : operations) {
            if (operation.equals("C")) {
                arr[index - 1] = 0;
                index--;
                continue;
            }
            if (operation.equals("D")) {
                arr[index] = 2 * arr[index - 1];
                index++;
                continue;
            }
            if (operation.equals("+")) {
                arr[index] = arr[index - 1] + arr[index - 2];
                index++;
                continue;
            }
            arr[index] = Integer.parseInt(operation);
            index++;
        }
        int res = 0;
        for (int i = 0; i < index; i++) {
            res += arr[i];
        }
        return res;
    }

    public static void main(String[] args) {
        calPoints(new String[]{"5", "2", "C", "D", "+"});
    }
}
